package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.handler.MessageContext;
import java.util.List;
@RestController
@RequestMapping("/menuController")
public class MenuController {
     @Reference
     private MenuService menuService ;
    @RequestMapping("/findLieBiao")
    public Result findLieBiao (){
        try{
            // 1）使用 帮助类(SecurityContextHolder)直接获取上下文对象(SecurityContext)
            SecurityContext securityContext = SecurityContextHolder.getContext();
            //2) 从上下文中获取用户信息
            //先获取认证对象
            Authentication authentication = securityContext.getAuthentication();
            //从认证对象中获取用户信息  (Principal : 重要的)
            Object principal = authentication.getPrincipal();
            //强转成User对象
            User user = (User) principal;
            String username = user.getUsername();
            List<Menu> list = menuService.findLieBiao(username);
           return new Result(true,"查询列表成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true,"查询列表失败");
        }

    }
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;

    }
    @RequestMapping("/add")
    public Result findAll(@RequestBody Menu menu){
       if (menu.getIcon()==null){
           return  new Result(false,"必须选择一个级别");
       }
       try{
           menuService.add(menu);
           return  new Result(true,"添加成功");
       }catch (RuntimeException e){
           e.printStackTrace();
           return  new Result(false,e.getMessage());
       }catch (Exception e){
           e.printStackTrace();
           return  new Result(false,"添加失败");
       }

    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Menu menu = menuService.findById(id);
        return  new Result(true,"菜单回显成功",menu);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,"菜单回显事变");
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try{
            menuService.edit(menu);
          return  new Result(true,"编辑菜单成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            return  new Result(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,"编辑菜单失败");
        }
    }
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menuList=menuService.findAll();
            return new Result(true,MessageConstant.QUERY_MENU_SUDCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/delById")
    public Result delById(Integer id){
        try{
            menuService.delById(id);
            return  new Result(true,"删除菜单成功");
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,"编辑菜单失败");
        }
    }

}
